# encoding: utf-8


"""

@author: tongzhenguo

@time: 2020/5/26

@desc:

封装sqlite操作
"""
import sqlite3

import pandas as pd


class SqliteDB:
    def __init__(self, **dbconfig):
        """初始化连接"""
        self.conn = sqlite3.connect(dbconfig["database"])

    def write_table(self, df, table_name, mode="replace"):
        """将dataframe写入sqlite表"""
        df.to_sql(table_name, self.conn, index=False, if_exists=mode)

    def sql_to_dataframe(self, sql):
        """执行sqlite sql,并将结果以dataframe返回"""
        return pd.read_sql(sql, self.conn)

    def manage(self, sql):
        """支持sqlite dml操作"""
        return self.conn.execute(sql)

    def write_csv(self, sql, path):
        df = self.sql_to_dataframe(sql)
        df.to_csv(path, index=False)


if __name__ == "__main__":
    pass
